/*
 * @Author: breakfly breakfly@126.com
 * @Date: 2024-01-18 11:00:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-05-08 14:34:28
 * @FilePath: /yl_kit/lib/src/sp_utils.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'dart:convert';
import 'dart:io';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:yl_kit/extension/src/yl_list.dart';
import 'package:yl_kit/extension/src/yl_map.dart';
import 'package:yl_kit/extension/src/yl_string.dart';
import 'yl_aes_encrypt.dart';
// import 'yl_rsa_encrypt.dart';

typedef ReadObj<T> = T Function(dynamic value);

class SpUtils {
  static SharedPreferences? _preferences;

  static Future<bool> getInstance() async {
    _preferences = await SharedPreferences.getInstance();
    return true;
  }

  static Future<bool> setObj<T>(String key, dynamic saveObj,
      {bool isEncrypt = true}) async {
    if (saveObj is Map) {
      saveObj = saveObj.removeNull();
    } else if (saveObj is List) {
      saveObj = saveObj.trim();
    }
    String value = jsonEncode(saveObj);
    value = gzipCompress(value);
    return setString(key, value, isEncrypt: isEncrypt);
  }

  static T getObj<T>(String key, ReadObj<T> readObj, {bool isEncrypt = true}) {
    String value = getString(key, isEncrypt: isEncrypt);

    if (value.isBase64) {
      value = gzipDecompress(value);
    }
    return readObj(value.isNotEmpty ? jsonDecode(value) : null);
  }

  static Future<bool> setString(String key, String value,
      {bool isEncrypt = true}) async {
    if (isEncrypt) {
      value = YLAESEncrypt.instance.encrypter(value);
    }
    return await _preferences?.setString(key, value) ?? false;
  }

  static String getString(String key,
      {String def = "", bool isEncrypt = true}) {
    String value = _preferences?.getString(key) ?? '';
    if (value.isEmpty) return def;
    if (!isEncrypt) return value;
    return YLAESEncrypt.instance.decrypter(value);
  }

  static Future<bool> setBool(String key, bool value) async {
    return await _preferences?.setBool(key, value) ?? false;
  }

  static bool getBool(String key) {
    return _preferences?.getBool(key) ?? false;
  }

  static Future<bool> setInt(String key, int value) async {
    return await _preferences?.setInt(key, value) ?? false;
  }

  static int getInt(String key) {
    return _preferences?.getInt(key) ?? 0;
  }

  static Future<bool> setDouble(String key, double value) async {
    return await _preferences?.setDouble(key, value) ?? false;
  }

  static double getDouble(String key) {
    return _preferences?.getDouble(key) ?? 0.0;
  }

  /// 删除所有的本地缓存
  static Future<bool> removeAll() async {
    return await _preferences?.clear() ?? false;
  }

  ///删除指定的缓存数据
  static Future<bool> remove(String key) async {
    return await _preferences?.remove(key) ?? false;
  }

  /// 删除某些缓存集合
  static Future<bool> removeKeys(List<String> keys) async {
    for (var key in keys) {
      await _preferences?.remove(key);
    }
    return true;
  }

  /// 删除某些集合外的数据
  static Future<bool> removeWithOut(List<String> keys) async {
    List<String> allKeys = List.from((_preferences?.getKeys().toList() ?? []));
    allKeys.removeWhere((element) => keys.contains(element));
    for (var key in allKeys) {
      await _preferences?.remove(key);
    }
    return true;
  }

  static String gzipCompress(String input) {
    final bytes = utf8.encode(input);
    final gzipBytes = gzip.encode(bytes); // GZip 压缩
    return base64.encode(gzipBytes);
  }

  static String gzipDecompress(String input) {
    final gzipBytes = base64.decode(input);
    final originalBytes = gzip.decode(gzipBytes); // GZip 解压
    return utf8.decode(originalBytes);
  }
}
